<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="代理商名称" prop="name">
                    <el-input
                        class="w-[180px]"
                        v-model="queryParams.name"
                        clearable
                        placeholder="请输入代理商名称"
                    />
                </el-form-item>
                <el-form-item label="手机号" prop="mobile">
                    <el-input
                        class="w-[180px]"
                        v-model="queryParams.mobile"
                        clearable
                        placeholder="请输入手机号"
                    />
                </el-form-item>
                <el-form-item label="级别" class="w-[180px]" prop="level">
                    <el-select v-model="queryParams.level" clearable placeholder="请选择级别">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in dictData.level"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="状态" class="w-[180px]" prop="disable">
                    <el-select v-model="queryParams.disable" clearable placeholder="请选择状态">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in dictData.system_disable"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['agents.agents/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column label="ID" prop="id" min-width="60" />>
                    <el-table-column label="头像" min-width="100">
                        <template #default="{ row }">
                            <el-avatar :size="50" :src="row.avatar"></el-avatar>
                        </template>
                    </el-table-column>
                    <el-table-column label="账号" prop="account" min-width="100" />
                    <el-table-column label="代理商名称" prop="name" min-width="100" />
                    <el-table-column label="代理商级别" prop="level" min-width="100">
                        <template #default="{ row }">
                            <dict-value :options="dictData.level" :value="row.level" />
                        </template>
                    </el-table-column>
                    <el-table-column label="手机号" prop="mobile" min-width="100" />
                    <el-table-column
                        label="联系地址"
                        prop="contact"
                        min-width="160"
                        show-tooltip-when-overflow
                    />
                    <el-table-column label="邮箱地址" prop="email" show-overflow-tooltip />
                    <el-table-column label="创建时间" prop="create_time" min-width="180" />
                    <el-table-column label="最近登录时间" prop="login_time" min-width="180" />
                    <el-table-column label="最近登录IP" prop="login_ip" min-width="120" />
                    <el-table-column label="状态" min-width="100" v-perms="['auth.admin/edit']">
                        <template #default="{ row }">
                            <el-switch
                                v-if="row.root != 1"
                                v-model="row.disable"
                                :active-value="0"
                                :inactive-value="1"
                                @change="changeStatus(row)"
                            />
                        </template>
                    </el-table-column>
                    <el-table-column label="返点" prop="rebate" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-perms="['agents.agents/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['agents.agents/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.agent_id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
    </div>
</template>

<script lang="ts" setup name="agentsLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiAgentsLists, apiAgentsDelete } from '@/api/agents'
import { timeFormat } from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
    level: '',
    mobile: '',
    name: '',
    disable: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ agent_id }) => agent_id)
}

// 获取字典数据
const { dictData } = useDictData('level,system_disable')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiAgentsLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (agent_id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiAgentsDelete({ agent_id })
    getLists()
}

getLists()
</script>

